const {
    parkingsModel
} = require('./models/parkingsModel')

module.exports.getParkings = async function (parking) {
    const total = (await parkingsModel.find({
        lessorsId: parking.lessorsId
    })).length;
    const pages = Math.ceil(total / parking.pageSize)
    const parkings = await parkingsModel
        .find({ lessorsId: parking.lessorsId })
        .populate({
            path: 'lessorsId'
        })
        .limit(parking.pageSize - 0) //限制的条数
        .skip((parking.currentPage - 1) * parking.pageSize) //跳过多少页
    return {
        parkings,
        total,
        pages
    }
}

module.exports.addParkings = async function (parking) {
    return await parkingsModel.create(parking)
}

module.exports.deleteParkings = async function (_id) {
    return await parkingsModel.deleteOne(_id);
}

/**
 * 修改
 * 
 */
module.exports.getParkingsId = async function (_id) {
    const data = await parkingsModel.find(_id);
    return data

}

module.exports.updateParkings = async function ({
    _id,
    address,
    number,
    property,
    cycle,
    price,
    state
}) {
    return await parkingsModel.updateOne({
        _id
    }, {
        address,
        number,
        property,
        cycle,
        price,
        state
    })
}
